package com.itheima.ithmshortlink.utils;

import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * @author andy
 * @version 1.0
 * @date 21/4/21
 * @description http报文解析 Utils
 */

@Slf4j
public class HttpUtils {

    private static final String WINDOWS = "windows";
    private static final String MAC = "mac";
    private static final String X_11 = "x11";
    private static final String ANDROID = "android";
    private static final String IPHONE = "iphone";
    private static final String EDGE = "edge";
    private static final String MSIE = "msie";

    /**
     * 获取操作系统
     *
     * @param request request
     * @return os
     */
    public static String getOs(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");

        String os;
        if (userAgent.toLowerCase().contains(WINDOWS)) {
            os = "windows";
        } else if (userAgent.toLowerCase().contains(MAC)) {
            os = "mac";
        } else if (userAgent.toLowerCase().contains(X_11)) {
            os = "unix";
        } else if (userAgent.toLowerCase().contains(ANDROID)) {
            os = "android";
        } else if (userAgent.toLowerCase().contains(IPHONE)) {
            os = "iPhone";
        } else {
            os = "other";
        }

        return os;
    }

    public static String getOs(String userAgent) {

        String os;
        if (userAgent.toLowerCase().contains(WINDOWS)) {
            os = "windows";
        } else if (userAgent.toLowerCase().contains(IPHONE)) {
            os = "iPhone";
        } else if (userAgent.toLowerCase().contains(MAC)) {
            os = "mac";
        } else if (userAgent.toLowerCase().contains(X_11)) {
            os = "unix";
        } else if (userAgent.toLowerCase().contains(ANDROID)) {
            os = "android";
        } else {
            os = "other";
        }

        return os;
    }

    /**
     * 获取浏览器及浏览器版本信息
     *
     * @param request request
     * @return browser
     */
    public static String getBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        String user = userAgent.toLowerCase();

        String browser = "";

        if (user.contains(EDGE)) {
            browser = (userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");
        } else if (user.contains(MSIE)) {
            String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
            browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
        } else if (user.contains("safari") && user.contains("version")) {
            browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]
                    + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
        } else if (user.contains("opr") || user.contains("opera")) {
            if (user.contains("opera")) {
                browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]
                        + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
            } else if (user.contains("opr")) {
                browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))
                        .replace("OPR", "Opera");
            }

        } else if (user.contains("chrome")) {
            browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
        } else if ((user.contains("mozilla/7.0")) || (user.contains("netscape6")) ||
                (user.contains("mozilla/4.7")) || (user.contains("mozilla/4.78")) ||
                (user.contains("mozilla/4.08")) || (user.contains("mozilla/3"))) {
            browser = "Netscape-?";

        } else if (user.contains("firefox")) {
            browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
        } else if (user.contains("rv")) {
            String ieVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");
            browser = "IE" + ieVersion.substring(0, ieVersion.length() - 1);
        } else if (userAgent.contains("micromessenger")) {
            browser = "微信内嵌浏览器";
        } else {
            browser = "other";
        }

        return browser;
    }

    /**
     * 获取浏览器及浏览器版本信息WithoutVersion
     *
     * @param request request
     * @return browser
     */
    public static String getBrowserWithoutVersion(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        String user = userAgent.toLowerCase();

        String browser;

        if (user.contains(EDGE)) {
            browser = "edge";
        } else if (user.contains(MSIE)) {
            String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
            browser = "msie";
        } else if (user.contains("micromessenger")) {
            browser = "wechat";
        } else if (user.contains("opr") || user.contains("opera")) {
            browser = "opera";
        } else if (user.contains("chrome")) {
            browser = "chrome";
        } else if (user.contains("safari") && user.contains("version")) {
            if ("android".equalsIgnoreCase(getOs(user))) {
                browser = "other";
            } else {
                browser = "safari";
            }
        } else if (user.contains("firefox")) {
            browser = "firefox";
        } else if ((user.contains("mozilla/7.0")) || (user.contains("netscape6")) ||
                (user.contains("mozilla/4.7")) || (user.contains("mozilla/4.78")) ||
                (user.contains("mozilla/4.08")) || (user.contains("mozilla/3"))) {
            browser = "netscape";

        } else if (user.contains("rv")) {
            browser = "ie";
        } else {
            browser = "other";
        }

        return browser;
    }

    /*
                浏览器	发布时间	所属公司
                Mosaic	1993 年 1 月 23 日	NCSA
                Netscape	1994 年 12 月	Mozilla
                Opera	1995 年 4 月	Opera Software ASA
                Internet Explorer	1995 年 8 月 16 日	Microsoft
                Kongqueror	1996 年 10 月 14 日	Kongqueror
                Safari	2003 年 1 月 7 日	Apple
                Firefox	2004 年 11 月 8 日	Mozilla
                Chrome	2008 年 9 月 2 日	Google
     */

    public static String getBrowserWithoutVersion(String userAgent) {
        String user = userAgent.toLowerCase();

        String browser;

        if (user.contains(EDGE) || user.contains("edg")) {
            browser = "edge";
        } else if (user.contains(MSIE)) {
            String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
            browser = "msie";
        } else if (user.contains("micromessenger")) {
            browser = "wechat";
        } else if (user.contains("xiaomi") || user.contains("miuibrowser") || user.contains("liebao")) {
            browser = "other";
        } else if (user.contains("mqqbrowser") || user.contains("qqbrowser")) {
            browser = "qq";
        } else if (user.contains("qihoobrowser")) {
            browser = "360";
        } else if (user.contains("opr") || user.contains("opera")) {
            browser = "opera";
        } else if (user.contains("chrome") || user.contains("crios")) {
            browser = "chrome";
        } else if (user.contains("firefox") || user.contains("fxios")) {
            browser = "firefox";
        } else if (user.contains("safari") && (user.contains("version") || user.contains("applewebkit"))) {
            if ("android".equalsIgnoreCase(getOs(user))) {
                browser = "other";
            } else {
                browser = "safari";
            }
        } else if ((user.contains("mozilla/7.0")) || (user.contains("netscape6")) ||
                (user.contains("mozilla/4.7")) || (user.contains("mozilla/4.78")) ||
                (user.contains("mozilla/4.08")) || (user.contains("mozilla/3"))) {
            browser = "netscape";

        } else if (user.contains("rv")) {
            browser = "ie";
        } else {
            browser = "other";
        }

        return browser;
    }

    public static String getDevice(HttpServletRequest request) {
        String agentString = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(agentString);
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        eu.bitwalker.useragentutils.DeviceType deviceType = operatingSystem.getDeviceType();

        switch (deviceType) {
            case COMPUTER:
                return "pc";
            case TABLET: {
                if (agentString.contains("Android")) {
                    return "androidPad";
                }
                if (agentString.contains("iOS")) {
                    return "iPad";
                }
                return "pad";
            }
            case MOBILE: {
                if (agentString.contains("Android")) {
                    return "android";
                }
                if (agentString.contains("iOS")) {
                    return "iPhone";
                }
                return "phone";
            }
            default:
                return "other";
        }

    }

    public static String getDevice(String agentString) {
        UserAgent userAgent = UserAgent.parseUserAgentString(agentString);
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        eu.bitwalker.useragentutils.DeviceType deviceType = operatingSystem.getDeviceType();

        switch (deviceType) {
            case COMPUTER:
                return "pc";
            case TABLET: {
                if (agentString.contains("Android")) {
                    return "androidPad";
                }
                if (agentString.contains("iOS")) {
                    return "iPad";
                }
                return "pad";
            }
            case MOBILE: {
                if (agentString.contains("Android")) {
                    return "android";
                }
                if (agentString.contains("iOS")) {
                    return "iPhone";
                }
                return "phone";
            }
            default:
                return "other";
        }

    }

}